package com.tanqidi.arcgisandroid.base.utils

import android.content.Context
import android.content.DialogInterface
import android.view.View
import android.widget.PopupMenu
import android.widget.Toast
import androidx.annotation.MenuRes
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.gson.GsonBuilder
import com.tanqidi.arcgisandroid.App
import com.tanqidi.arcgisandroid.R

fun showToast(any:Any) {
    Toast.makeText(App.app, any.toString(), Toast.LENGTH_SHORT).show()
}

fun getSPUtils() : SPUtils {
    return SPUtils.getInstance()
}

fun getString(id: Int) : String {
    return App.app!!.resources.getString(id)
}

fun showMenu(v: View, @MenuRes menuRes: Int, listener: PopupMenu.OnMenuItemClickListener) {
    val popup = PopupMenu(v.context, v)
    popup.menuInflater.inflate(menuRes, popup.menu)
    popup.setOnMenuItemClickListener(listener)
    popup.setOnDismissListener {}
    popup.show()
}

fun showSingleChoiceDialog(context: Context, defaultIndex:Int = -1, title:String, list: List<String>, callBack: CallBack){
    var checkedItem = defaultIndex
    MaterialAlertDialogBuilder(context)
        .setTitle(title)
        .setPositiveButton(context.resources.getString(R.string.ok)) { dialog, which ->
            if(checkedItem != defaultIndex){
                callBack.callBack(checkedItem)
            }
        }
        .setSingleChoiceItems(list.toTypedArray(), checkedItem) { dialog, which ->
            checkedItem = which
        }
        .show()
}

fun showConfirmDialog(context: Context, title: String, message: String, onClickListener: DialogInterface.OnClickListener) {
    MaterialAlertDialogBuilder(context)
        .setTitle(title)
        .setMessage(message)
        .setNeutralButton(getString(R.string.cancel)) { dialog, which ->
            // Respond to neutral button press
        }
        .setPositiveButton(getString(R.string.accept), onClickListener)
        .show()
}
fun showConfirmDialog(context: Context, title: String, message: String, cancelable:Boolean, onClickListener: DialogInterface.OnClickListener, cancelOnClickListener: DialogInterface.OnClickListener) {
    MaterialAlertDialogBuilder(context)
        .setTitle(title)
        .setCancelable(cancelable)
        .setMessage(message)
        .setNeutralButton(getString(R.string.cancel), cancelOnClickListener)
        .setPositiveButton(getString(R.string.accept), onClickListener)
        .show()
}

class AppUtils {
}